package com.gzc.just.play.last.war.centralserver.battle.model;

import java.util.HashSet;
import java.util.Set;

public class Battle {
    private long battleId;
    private long creatorId;
    private String battleName;
    private int mapId;
    private int maxPlayers;
    private BattleStatus status;
    private Set<Long> players;
    private long createTime;
    private long startTime;
    private long finishTime;
    private BattleResult result;
    
    public Battle() {
        this.players = new HashSet<>();
        this.status = BattleStatus.WAITING;
    }
    
    // Getters and Setters
    public long getBattleId() {
        return battleId;
    }
    
    public void setBattleId(long battleId) {
        this.battleId = battleId;
    }
    
    public long getCreatorId() {
        return creatorId;
    }
    
    public void setCreatorId(long creatorId) {
        this.creatorId = creatorId;
    }
    
    public String getBattleName() {
        return battleName;
    }
    
    public void setBattleName(String battleName) {
        this.battleName = battleName;
    }
    
    public int getMapId() {
        return mapId;
    }
    
    public void setMapId(int mapId) {
        this.mapId = mapId;
    }
    
    public int getMaxPlayers() {
        return maxPlayers;
    }
    
    public void setMaxPlayers(int maxPlayers) {
        this.maxPlayers = maxPlayers;
    }
    
    public BattleStatus getStatus() {
        return status;
    }
    
    public void setStatus(BattleStatus status) {
        this.status = status;
    }
    
    public Set<Long> getPlayers() {
        return new HashSet<>(players);
    }
    
    public void setPlayers(Set<Long> players) {
        this.players = new HashSet<>(players);
    }
    
    public long getCreateTime() {
        return createTime;
    }
    
    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }
    
    public long getStartTime() {
        return startTime;
    }
    
    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }
    
    public long getFinishTime() {
        return finishTime;
    }
    
    public void setFinishTime(long finishTime) {
        this.finishTime = finishTime;
    }
    
    public BattleResult getResult() {
        return result;
    }
    
    public void setResult(BattleResult result) {
        this.result = result;
    }
    
    public boolean addPlayer(long userId) {
        return players.add(userId);
    }
    
    public boolean removePlayer(long userId) {
        return players.remove(userId);
    }
    
    public boolean hasPlayer(long userId) {
        return players.contains(userId);
    }
    
    public boolean isFull() {
        return players.size() >= maxPlayers;
    }
    
    public int getPlayerCount() {
        return players.size();
    }
    
    public int getRemainingSlots() {
        return maxPlayers - players.size();
    }
    
    public long getDuration() {
        if (startTime <= 0) {
            return 0;
        }
        long endTime = finishTime > 0 ? finishTime : System.currentTimeMillis();
        return endTime - startTime;
    }
    
    @Override
    public String toString() {
        return "Battle{" +
                "battleId=" + battleId +
                ", creatorId=" + creatorId +
                ", battleName='" + battleName + '\'' +
                ", mapId=" + mapId +
                ", maxPlayers=" + maxPlayers +
                ", status=" + status +
                ", players=" + players.size() + "/" + maxPlayers +
                ", result=" + result +
                '}';
    }
    
    // Inner BattleResult class
    public static class BattleResult {
        private long winnerId;
        private int winnerScore;
        private int loserScore;
        private long battleDuration;
        
        public BattleResult() {
        }
        
        public BattleResult(long winnerId, int winnerScore, int loserScore, long battleDuration) {
            this.winnerId = winnerId;
            this.winnerScore = winnerScore;
            this.loserScore = loserScore;
            this.battleDuration = battleDuration;
        }
        
        public long getWinnerId() {
            return winnerId;
        }
        
        public void setWinnerId(long winnerId) {
            this.winnerId = winnerId;
        }
        
        public int getWinnerScore() {
            return winnerScore;
        }
        
        public void setWinnerScore(int winnerScore) {
            this.winnerScore = winnerScore;
        }
        
        public int getLoserScore() {
            return loserScore;
        }
        
        public void setLoserScore(int loserScore) {
            this.loserScore = loserScore;
        }
        
        public long getBattleDuration() {
            return battleDuration;
        }
        
        public void setBattleDuration(long battleDuration) {
            this.battleDuration = battleDuration;
        }
        
        @Override
        public String toString() {
            return "BattleResult{" +
                    "winnerId=" + winnerId +
                    ", winnerScore=" + winnerScore +
                    ", loserScore=" + loserScore +
                    ", battleDuration=" + battleDuration +
                    '}';
        }
    }
}
